/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import Shared.Server;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

/**
 *
 * @author Thor Erik
 */
public class Tray extends JFrame {

    private  Long startTime = System.currentTimeMillis()/1000;

    private SystemTray systemTray = SystemTray.getSystemTray();    //If systemamanger is used, be sure to grant accessystemtray to the list! (check <---- this method)
    private Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Pictures/trayicon.png"));
    private PopupMenu popup = new PopupMenu("Administrativt støtteverktøy for CCD");
    Server server;

    /*
     * Under here we can add more and more items to our menu!
     */
    private MenuItem exit = new MenuItem("Avslutt");
    private MenuItem uptime = new MenuItem("Oppetid");
    private TrayIcon icon = new TrayIcon(img, "CCD", popup);
    private boolean enablecheck = false;

    public Tray(Server server) throws RemoteException {
        this.server = server;
        addActionListenerToExit();
        addActionListenerToUptime();
        addPopupItems();
        try {
            systemTray.add(icon);
        } catch (AWTException ex) {
            Logger.getLogger(Tray.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void addPopupItems() {
        getPopup().add(getUptime());
        getPopup().add(getExit());

    }

    private void addActionListenerToUptime() {
        getUptime().addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
        Long currentTime = System.currentTimeMillis()/1000;
        Long calculatedUptime = currentTime - getStartTime();
        JOptionPane.showMessageDialog(null, "Serveren sin oppetid er på: "+calculatedUptime +" sekunder.");
        }
        });
    }

    private void addActionListenerToExit() {
        getExit().addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(isEnablecheck()==true){
                try {
                    Naming.unbind(ServerImpl.getNaming());
                } catch (RemoteException ex) {
                    Logger.getLogger(Tray.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NotBoundException ex) {
                    Logger.getLogger(Tray.class.getName()).log(Level.SEVERE, null, ex);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(Tray.class.getName()).log(Level.SEVERE, null, ex);
                }
                }
                System.exit(0);
            }
        });
    }

    private TrayIcon getIcon() {
        return icon;
    }

    private void setIcon(TrayIcon icon) {
        this.icon = icon;
    }

    private Image getImg() {
        return img;
    }

    private void setImg(Image img) {
        this.img = img;
    }

    private PopupMenu getPopup() {
        return popup;
    }

    private void setPopup(PopupMenu popup) {
        this.popup = popup;
    }

    private SystemTray getSystemTray() {
        return systemTray;
    }

    private void setSystemTray(SystemTray systemTray) {
        this.systemTray = systemTray;
    }

    public boolean isEnablecheck() {
        return enablecheck;
    }

    public void setEnablecheck(boolean enablecheck) {
        this.enablecheck = enablecheck;
    }

    private MenuItem getExit() {
        return exit;
    }

    private void setExit(MenuItem exit) {
        this.exit = exit;
    }

    public Long getStartTime() {
        return startTime;
    }

    public void setStartTime(Long startTime) {
        this.startTime = startTime;
    }

    public MenuItem getUptime() {
        return uptime;
    }

    public void setUptime(MenuItem uptime) {
        this.uptime = uptime;
    }

}
